﻿using CHSystemApi.Framework.Extension;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;

namespace CHSystemApi.Framework.Base
{
    /// <summary>
    /// 自定义控制器基类
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 登录信息
        /// </summary>
        protected LoginInfo LoginInfo
        {
            get
            {
                return HttpContext.Items["LoginInfo"] as LoginInfo;
            }
        }

        protected BaseResponse OK(object result)
        {
            return new BaseResponse<object>()
            {
                code = Extension.StatusCode.OK,
                data = result,
                msg = "成功",
                success = true
            };
        }

        protected BaseResponse Error()
        {
            return new BaseResponse()
            {
                code = Extension.StatusCode.Internal_Server_Error,
                msg = "失败",
                success = false
            };
        }

        protected BaseResponse<T> AjaxResult<T>(T result, Func<T, StatusCode> func)
        {
            StatusCode code = func?.Invoke(result) ?? Extension.StatusCode.Internal_Server_Error;
            return new BaseResponse<T>()
            {
                code = code,
                data = result,
                msg = "",
                success = code == Extension.StatusCode.OK
            };
        }
    }
}
